package com.dandev.cmdemo.form;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import org.codehaus.jackson.annotate.JsonIgnore;
import org.hibernate.validator.constraints.NotEmpty;

/**
 * A message or note with a type and a rating.
 * @author Dan
 *
 */
@Entity
@Table(name="communique")
public class Communique {
	
	@Id
    @Column(name="id")
    @GeneratedValue
	private Long id;
	
	@ManyToOne
	@JoinColumn (name="contact_id")
	private Contact parentContact;
	
	@Column(name="message")
	@NotEmpty
	private String message;
	
	@Column(name="created")
	private Date created = new Date();
	
	@Column(name="ranking_id")
	private Long ranking;
	
	@Column(name="comm_type_id")
	private Long commTypeId;
	
	private transient Long parentContactId;
	
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	
	public Contact getParentContact() {
		return parentContact;
	}
	public void setParentContact(Contact parentContact) {
		this.parentContact = parentContact;
		if(parentContact != null) this.parentContactId = parentContact.getId();
	}
	public String getMessage() {
		return message;
	}
	public void setMessage(String message) {
		this.message = message;
	}
	public Date getCreated() {
		return created;
	}
	public void setCreated(Date created) {
		this.created = created;
	}
	
	/**
	 * @deprecated use initBinder in controller
	 * @return a formated String.
	 */
	@Deprecated
	public String getFormatedCreated() {
		SimpleDateFormat sdf = new SimpleDateFormat("MMM d, yyyy h:m a");
		return sdf.format(created);
	}
	public Long getRanking() {
		return ranking;
	}
	public void setRanking(Long ranking) {
		this.ranking = ranking;
	}
	public Long getParentContactId() {
		return parentContactId;
	}
	public void setParentContactId(Long parentContactId) {
		this.parentContactId = parentContactId;
	}
	
	public Long getCommTypeId() {
		return commTypeId;
	}
	public void setCommTypeId(Long commTypeId) {
		this.commTypeId = commTypeId;
	}
	
	/**
	 * To be used with a persistence manager.  After a instance is loaded from the 
	 * persistence manager use this method update instance.
	 * @param other updated from.
	 */
	public void update(Communique other) {
		setMessage(other.getMessage());
		setCreated(other.getCreated());
		setRanking(other.getRanking());
		setCommTypeId(other.getCommTypeId());
	}
	
	@Override
	public String toString() {
		return "Communique [id=" + id + ", message=" + message + ", created="
				+ created + ", ranking=" + ranking + ", commTypeId="
				+ commTypeId + ", parentContactId=" + parentContactId + "]";
	}
	
	/**
	 * was just experimenting 
	 * @return a JSON string
	 */
	public String toJSON() {
		return "{\"id\":" + getId() + ", \"message\": \"" + getMessage() + "\", \"created\": \""
				+ getFormatedCreated() + "\", \"ranking\": " + getRanking() + ", \"commTypeId\":"
				+ getCommTypeId() + "}";
	}
	
	

}
